home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000122_icon-group-sender _Fri Jun 4 08:01:40 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA19355
  4.     for icon-group-addresses; Fri, 4 Jun 1999 08:01:01 -0700 (MST)
  5. Message-Id: <199906041501.IAA19355@baskerville.CS.Arizona.EDU>
  6. Delivered-To: icon-group@silliac.cs.arizona.edu
  7. From: swampler@noao.edu (Steve Wampler)
  8. Date: Thu, 3 Jun 1999 21:17:38 MST
  9. To: icon-group@optima.CS.Arizona.EDU
  10. Subject: Re: Find the longest matching substrings
  11. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  12. Status: RO
  13.  
  14. On Jun 3 at  4:59pm, de yang writes:
  15. } Dear Sirs,
  16. } I am working on a project that will mark the inserted or deleted phases
  17. } between two strings.  I am wondering whether you know any library
  18. } procedure that will anchor ( or report ) the longest phases that exists
  19. } in both  strings.  
  20. } For example, assume that we have two strings:
  21. }     old:="other strings ab cd e and more substrings"
  22. }     new:="some more substrings ab cd ab cd e"
  23. } This function should return phases and position in both strings, i.e.
  24. } [15,  28, "ab cd e"]  for "ab cd e" is the longest phase that matched (
  25. } it has three continuous matching elements ).
  26.  
  27. I've send a solution to De Yang, but it occurred to me that others
  28. might like to see it (and improve on it - it's a bit, uh, ugly).
  29. This solutions makes two assumptions:
  30.  
  31.    (1) There is a simple definition for what a phrase element is
  32.        (in fact, it assumes an element is a sequence of letters!)
  33.  
  34.    (2) Punctuation should be considered when matching phrases, so
  35.        "that that is is that that is not is not" and
  36.        "that that is, is. that that is not, is not." do not
  37.        match each other (instead, the longest common phrase in
  38.        this case is "that that is not")
  39.  
  40. I have another solution that drops assumption 2, if any one is
  41. interested.
  42.  
  43. Anyway, here's a solution, plus a simple test driver...
  44.  
  45. -----
  46. procedure main(args)
  47.  
  48.     if result := longPhrase(read(),read()) then
  49.         write("[",result[1],",",result[2],",\"",result[3],"\"]")
  50.  
  51. end
  52.  
  53. # Find longest phrase common to s1 and s2.  Fails if no common phrase.
  54. #
  55. #  Assumes simple definition for a phrase (sequence of letters)
  56. #  Assumes punctuation is to be considered when matching phrases
  57. #
  58. procedure longPhrase(s1,s2)
  59.     static pChars
  60.     initial pChars := &letters        # chars legal in phrase element
  61.  
  62.     maxlen := 0
  63.  
  64.     every j := findPhrase(s1[ (i := pStart(s1,pChars)) :
  65.                               (k := rpStop(s1,pChars))  ], s2, pChars) do {
  66.  
  67.         if maxlen <:= numPhrases(s1[i:k], pChars) then {
  68.             write(s1[i:k])
  69.             saveStart1 := i
  70.             saveStop1  := k
  71.             saveStart2 := j
  72.             }
  73.  
  74.        }
  75.  
  76.     return [\saveStart1, saveStart2, s1[saveStart1:saveStop1]]
  77. end
  78.  
  79. procedure pStart(s, pc)            # generate start of all phrases in s
  80.     s ? {
  81.         while tab(i := upto(pc)) do {
  82.             tab(many(pc))
  83.             suspend i
  84.             }
  85.         }
  86. end
  87.  
  88. procedure rpStop(s, pc)            # generates end of all phrases in s,
  89.                                         #   starting from right of string
  90.     suspend *s+2 - pStart(reverse(s), pc)
  91. end
  92.  
  93. procedure numPhrases(s, pc)        # Count number of phrases in s
  94.     local count
  95.  
  96.     count := 0
  97.     every pStart(s,pc) do count +:= 1
  98.  
  99.     return count
  100. end
  101.  
  102. procedure findPhrase(p, s, pc)        # find phrase p in s
  103.     s ? {
  104.         while tab(i := upto(pc)) do {
  105.             if match(p) then suspend i
  106.             tab(many(pc))
  107.             }
  108.         }
  109. end
  110.  
  111. -- 
  112. --
  113. Steve Wampler-  SOLIS Project, National Solar Observatory
  114. swampler@noao.edu
  115.